/**
 * 正则表达式校验
 * @param reg 正则表达式
 * @param message 错误提示
 * @param event 校验事件，默认 blur
 * @returns {{validator: validator, trigger: string}}
 */
export function regexValidator(reg, message, event = "blur") {
  return {
    validator: (rule, value, callback) => {
      if (value && value.trim() && !reg.test(value)) {
        callback(new Error(message));
      } else {
        callback();
      }
    },
    trigger: event,
  };
}

/**
 * 长度校验规则
 * @param minLength 最小长度
 * @param maxLength 最大长度
 * @param message 错误提示
 * @param event  校验事件，默认 blur
 * @returns {{validator: validator, trigger: string}}
 */
export function lengthValidator(minLength, maxLength, message, event = "blur") {
  return {
    validator: (rule, value, callback) => {
      if (value && (value.length < minLength || value.length > maxLength)) {
        callback(new Error(message));
      } else {
        callback();
      }
    },
    trigger: event,
  };
}

/**
 * URL地址
 * @param {*} s
 */
export function isURL(s) {
  return /^http[s]?:\/\/.*/.test(s);
}

export default {
  // 必录项校验
  required: (label) => {
    return { required: true, message: `${label}不能为空`, trigger: "blur" };
  },

  // 用户名校验规则
  username: (required) => {
    let arr = [];
    if (required) {
      arr.push({ required: true, message: "用户名不能为空", trigger: "blur" });
    }
    arr.push(regexValidator(/^\w+$/, "用户名只能输入数字字母下划线"));
    arr.push(
      regexValidator(/^\s*[\s\S]{5,10}\s*$/, "用户名长度范围必须在5~10之间")
    );
    return arr;
  },

  // 用户姓名校验规则
  fullname: (required) => {
    let arr = [];
    if (required) {
      arr.push({
        required: true,
        message: "用户姓名不能为空",
        trigger: "blur",
      });
    }
    arr.push(
      regexValidator(
        /^[\u4E00-\u9FA5A-Za-z0-9]{2,10}$/,
        "用户姓名只能为2~5位的中文、字母和数字"
      )
    );
    return arr;
  },

  // 邮箱
  email: (required) => {
    let arr = [];
    if (required) {
      arr.push({ required: true, message: "邮箱不能为空", trigger: "blur" });
    }
    arr.push(lengthValidator(0, 50, "邮箱长度不能超过50个字符"));
    arr.push(
      regexValidator(
        /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/,
        "邮箱格式错误"
      )
    );
    return arr;
  },

  // 手机号码
  mobile: (required) => {
    let arr = [];
    if (required) {
      arr.push({
        required: true,
        message: "手机号码不能为空",
        trigger: "blur",
      });
    }
    arr.push(regexValidator(/^1[0-9]{10}$/, "手机号码格式错误"));
    return arr;
  },

  // 电话号码
  phone: (required) => {
    let arr = [];
    if (required) {
      arr.push({
        required: true,
        message: "电话号码不能为空",
        trigger: "blur",
      });
    }
    arr.push(regexValidator(/^([0-9]{3,4}-)?[0-9]{7,8}$/, "电话号码格式错误"));
    return arr;
  },

  // 代码校验
  code: (required) => {
    let arr = [];
    if (required) {
      arr.push({ required: true, message: "代码不能为空", trigger: "blur" });
    }
    arr.push(
      regexValidator(/([a-zA-Z0-9]|:){5,10}/g, "代码只能包含数字、字母和:字符")
    );
    return arr;
  },

  // 组件名称校验
  name: (required, label, minLength, maxLength) => {
    let arr = [];
    if (required) {
      arr.push({
        required: true,
        message: `${label}不能为空`,
        trigger: "blur",
      });
    }
    arr.push(
      regexValidator(
        /^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$/,
        `${label}只能为2~20位的中文、字母和数字`
      )
    );
    return arr;
  },
};
